﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp.Test
{
    internal class TestDateTime
    {
        public static void Test()
        {
            //TestManualResetEvent();

            //DateTime.TryParse()
            //yyyy-MM-dd_HH_mm_ss.fff
            var time = DateTime.Parse("2013-10-15 23:14:15.45");
            var timeStr1 = TimeToString(ref time, "yyyy-MM-dd HH:mm:ss.fffffff");//2013-10-15 23:14:15.4560000
            var timeStr2 = TimeToString(ref time, "yyyy-MM-dd HH:mm:ss.FFFFFF");//2013-10-15 23:14:15.456
            var timeStr3 = TimeToString(ref time, "yyyy-MM-dd HH:mm:SS.fffffff");//2013-10-15 23:14:SS.4560000
            var timeStr4 = TimeToString(ref time, "yyyy-MM-dd HH:MM:ss.fffffff");//2013-10-15 23:10:15.4560000
            var timeStr5 = TimeToString(ref time, "yyyy-MM-dd hh:mm:ss.fffffff AM");//2013-10-15 11:14:15.4560000 AM  //12小时制 AM PM am pm A.M P.M a.m p.m
            var timeStr6 = TimeToString(ref time, "yyyy-MM-DD HH:mm:ss.fffffff");//2013-10-DD 23:14:15.4560000
            var timeStr7 = TimeToString(ref time, "yyyy-mm-dd HH:mm:ss.fffffff");//2013-14-15 23:14:15.4560000
            var timeStr8 = TimeToString(ref time, "YYYY-MM-dd HH:mm:ss.fffffff");//YYYY-10-15 23:14:15.4560000
            var timeStr9 = TimeToString(ref time, "yy-MM-dd HH:mm:ss.fffffff");//13-10-15 23:14:15.4560000

            int index = timeStr9.IndexOf("张三");
            var sb = new StringBuilder();



            //sb.ToString();
            //string ToString(int startIndex, int length)

            //sb.Append();
            //Append(string value, int startIndex, int count)
            //sb.AppendLine()
            //sb.Insert(int index, string value, int count)

            //sb.Remove(int startIndex, int length)
            //sb.Clear();

            //sb.AppendFormat(string format, object arg0)
            //sb.Replace(string oldValue, string newValue, int startIndex, int count)
            //Replace(char oldChar, char newChar)
            //Replace(char oldChar, char newChar, int startIndex, int count)

            //Stopwatch sw = Stopwatch.StartNew();
            //Thread.Sleep(2000);
            //sw.Stop();
            //var ts1 = sw.ElapsedMilliseconds;
            ////sw.Start();
            //sw.Restart();
            //Thread.Sleep(2000);
            //sw.Stop();
            //var ts2 = sw.ElapsedMilliseconds;


        }

        private static string TimeToString(ref DateTime time, string format)
        {
            try
            {
                return time.ToString(format);
            }
            catch (Exception ex)
            {
                return "无效";
            }
        }

        private static void TestManualResetEvent()
        {
            Stopwatch stopwatch = new Stopwatch();
            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            int waitMilliseconds = 2000;

            //Task.Factory.StartNew(() =>
            //{
            //    Thread.Sleep(1000);
            //    manualResetEvent.Set();
            //});
            stopwatch.Restart();
            manualResetEvent.Set();//Set()之后 ，必须Reset()，否则WaitOne直接返回
                                   //manualResetEvent.Reset();
            bool ret1 = manualResetEvent.WaitOne(waitMilliseconds);
            stopwatch.Stop();
            var em1 = stopwatch.ElapsedMilliseconds;

            stopwatch.Restart();
            bool ret2 = manualResetEvent.WaitOne(waitMilliseconds);
            stopwatch.Stop();
            var em2 = stopwatch.ElapsedMilliseconds;

            stopwatch.Restart();
            bool ret3 = manualResetEvent.WaitOne(waitMilliseconds);
            stopwatch.Stop();
            var em3 = stopwatch.ElapsedMilliseconds;
        }

        private static void TestSpinLock()
        {
            var sl = new SpinLock();
            bool lockTabken = false;
            sl.Enter(ref lockTabken);
            if (lockTabken)
            {
                //...
                sl.Exit();
            }
        }



        private static void TestSpinUntil()
        {
            int waitMilliseconds = 2000;
            Stopwatch stopwatch = Stopwatch.StartNew();
            int value = 1;
            bool ret = SpinWait.SpinUntil(() =>
            {
                Console.WriteLine("value++");
                value++;
                if (value++ >= int.MaxValue)
                {
                    return true;
                }

                return false;
            }, waitMilliseconds);
            stopwatch.Stop();
            Console.WriteLine(stopwatch.ElapsedMilliseconds);
        }
    }
}
